package cn.gzsxt.music.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.gzsxt.music.mapper.CDMapper;
import cn.gzsxt.music.query.CDQueryObject;
import cn.gzsxt.music.service.CDService;
/**
 * 专辑列表服务层
 * @author 揉麻薯的tamako
 *
 */
@Service
public class CDServiceImpl implements CDService {
	@Autowired
	private CDMapper cdMapper;
	
	/**
	 * 分页后传到页面的数据
	 * total:总数据量
	 * rows:每页的数据
	 */
	@Override
	public Map<String, Object> cdList(CDQueryObject cqo) {
		// TODO Auto-generated method stub
		if(StringUtils.isNotBlank(cqo.getSinger_name())) {
			Map<String, String> singerName = new HashMap<>();
			singerName.put("singer_name", cqo.getSinger_name());
			List<Map<String, Object>> singerId = this.selectSingerId(singerName);
			if(singerId != null && singerId.size() != 0) {
				StringBuilder str = new StringBuilder();
				for(Map<String, Object> map : singerId) {
					str.append(map.get("singer_id"));
					str.append(",");
				}
				String id = new String(str.substring(0, str.length()-1).toString());
				System.out.println(id);
				cqo.setSinger_id(id);
			}
		}
		
		Map<String, Object> map = new HashMap<>();
		map.put("total", cdMapper.selectAllCDPage(cqo));
		map.put("rows", cdMapper.selectAllCD(cqo));
		return map;
	}
	
	/**
	 * 根据用户输入的专辑名进行模糊查询
	 * @param cdName cdName 查询专辑的名字参数和分页信息
	 * @return 返回专辑的数量和专辑信息
	 */
	/*@Override
	public Map<String, Object> cdSelect(Map<String, Object> cdName) {
		// TODO Auto-generated method stub
		Map<String, Object> map = cdMapper.selectCD(cdName);
		return map;
	}*/
	
	/**
	 * 添加一个新的专辑
	 * @param map 新的专辑数据
	 */
	@Override
	public int addCD(Map<String, Object> map) {
		// TODO Auto-generated method stub
		int row = cdMapper.addCD(map);
		return row;
	}
	
	/** 修改一个专辑
	 * @param map 专辑数据 
	 */
	@Override
	public int updateCD(Map<String, Object> map) {
		// TODO Auto-generated method stub
		int row = cdMapper.updateCD(map);
		return row;
	}
	
	/**
	 * 查找所有的歌手
	 */
	@Override
	public List<Map<String, Object>> selectSinger() {
		// TODO Auto-generated method stub
		List<Map<String, Object>> singerList = cdMapper.selectSinger();
		return singerList;
	}
	
	/**
	 * 根据id删除专辑
	 */
	@Override
	public int deleteCD(Map<String, Object> map) {
		// TODO Auto-generated method stub
		int row = cdMapper.deleteCD(map);
		return row;
	}
	
	/**
	 * 根据姓名查找歌手的id
	 * @param singerName 歌手姓名
	 * @return 返回查找到的歌手id
	 */
	@Override
	public List<Map<String, Object>> selectSingerId(Map<String, String> singerName) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> singerId = cdMapper.selectSingerId(singerName);
		
		return singerId;
	}

}
